import inspect
from functools import wraps


# 第一题
def param_inspect(fn):
    @wraps(fn)
    def inner(*args, **kwargs):
        sig = inspect.signature(fn)
        param = sig.parameters
        typ = list(param.values())
        if args:
            for i, x in enumerate(args):
                if not isinstance(x, typ[i].annotation):
                    raise TypeError('{} is {} not {}'.format(x, type(x), typ[i]))
        if kwargs:
            for k, v in kwargs.items():
                if not isinstance(v, param[k].annotation):
                    raise TypeError('{} is {} not {}'.format(v, type(v), param[k].annotation))
        return fn(*args, **kwargs)
    return inner


@param_inspect
def functer(var1: list, var2: int):
    for i in var1:
        if i % var2 == 0:
            yield i


for i in functer(list(range(-10, 10)), 3):
    print(i)


# 第二题
@param_inspect
def same_str(var1: str, var2: str):
    length1 = len(var1)
    length2 = len(var2)
    xindex = 0
    xmax = 0
    l = [[0] *length1 for i in range(length2)]
    for i, x in enumerate(var2):
        for j, y in enumerate(var1):
            if x == y:
                if i == 0 or j == 0:
                    l[i][j] = 1
                else:
                    l[i][j] = l[i-1][j-1] + 1
                if l[i][j] > xmax:
                    xmax = l[i][j]
                    xindex = j
    print(*l, sep='\n')
    start = xindex + 1 - xmax
    end = xindex + 1
    return var1[start:end]


print(same_str('avcsesa', 'avsesa'))

# 结果都是对的，第二道题目考虑一下如果有2个相同长度的子串，该怎么处理，例如
# avcsesa和 avcsacsesa